package masterjava.common.client.gxt.base.util;

import com.extjs.gxt.ui.client.js.JsObject;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.json.client.JSONArray;

/**
 * User: gkislin
 * Date: 7/21/11
 */
public class ScriptUtil {
    public static native JavaScriptObject evalScriptObject(String jsObjectName) /*-{
        return eval('$wnd.' + jsObjectName);
    }-*/;

    // Workaround, work correctly with highstock only via inline script
    public static native JavaScriptObject evalJsonObject(String jsonObject) /*-{
//        return eval("["+jsonObject+"]")[0];
        return $wnd.evalJson(jsonObject);
    }-*/;

    public static JsObject getJsObject(JavaScriptObject jso) {
        return new WrapJsObject(jso);
    }

    private static class WrapJsObject extends JsObject {
        private WrapJsObject(JavaScriptObject jso) {
            jsObject = jso;
        }
    }

    public static JSONArray copy(JSONArray src) {
        JSONArray dest = new JSONArray();
        for (int i = 0; i < src.size(); i++) {
            dest.set(i, src.get(i));
        }
        return dest;
    }
}
